home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 08 - 1992 / 08.02 Jun 92 / Close File FKEY / CloseMain.p < prev    next >
Encoding:
Text File  |  1989-04-03  |  2.3 KB  |  80 lines  |  [TEXT/PJMM]

  1. {======================}
  2. unit FKEY;
  3. {======================}
  4. interface
  5. {======================}
  6.     procedure Main;
  7. {======================}
  8. implementation
  9. {======================}
  10.     procedure Main;
  11.         var
  12.             dlgOrigin: Point;            {Top-left corner of dialog box}
  13.             theReply: SFReply;            {Data returned by SFGet dialog}
  14.             theFile: INTEGER;            {Reference number of file}
  15.             resultCode: OSErr;            {I/O error code}
  16.             TypeList: SFTypeList;        {Type List to search for (unused) }
  17.             Block: ParamBlockRec;    {Used to get file reference number}
  18.             WIND: WindowPtr;            {Display Window }
  19.             bounds: Rect;                {window rectangle Rectangle }
  20.             OldPort: GrafPtr;            {Graf Port set upon entry}
  21.             dummy: LONGINT;            {parameter of Delay}
  22. {======================}
  23. { CenterDraw                                                }
  24. { Input: String to Draw , line number to draw on.        }
  25. { (String is centered horizontally on the window.)        }
  26. {======================}
  27.         procedure CenterDraw (s: Str255; y: INTEGER);
  28.             var
  29.                 x: INTEGER;
  30.         begin
  31.             with WIND^.portRect do
  32.                 begin
  33.                     x := (right + left) div 2 - StringWidth(s) div 2;
  34.                     MoveTo(x, y * 16);
  35.                 end;
  36.             DrawString(s);
  37.         end;
  38. {======================}
  39.     begin
  40.         GetPort(OldPort);
  41.         SetRect(bounds, 85, 40, 420, 100);
  42.         WIND := NewWindow(nil, bounds, '', TRUE, dBoxProc, pointer(-1), FALSE, 0);
  43.         SetPort(WIND);
  44.         TextFace([underline]);
  45.         CenterDraw('Close File FKEY', 1);
  46.         TextFace([]);
  47.         CenterDraw('Select the file you wish to close.', 2);
  48.  
  49.         SetPt(dlgOrigin, 85, 120);            {Set up dialog origin}
  50.         SFGetFile(dlgOrigin, '', nil, -1, TypeList, nil, TheReply);
  51.  
  52.         with TheReply do
  53.             if good then
  54.                 begin
  55. {Try to open file}
  56.                     resultCode := FSOpen(fname, vRefNum, theFile);
  57. {if it was already open, get the file ref number}
  58.                     if resultCode = opWrErr then
  59.                         with Block do
  60.                             begin
  61.                                 ioNamePtr := @fName;
  62.                                 ioVRefNum := vRefNum;
  63.                                 ioFDirIndex := -1;
  64.                                 resultCode := PBGetFInfo(@Block, false);  {Get Finder info}
  65.                                 theFile := ioFRefNum;
  66.                                 CenterDraw(Concat('“', fName, '” is now closed.'), 3);
  67.                             end
  68.                     else
  69.                         CenterDraw(Concat('“', fName, '” was not open.'), 3);
  70. {Do the actual closing... regardless if it was open before the FKEY or not}
  71.                     ResultCode := fsClose(theFile);
  72.                 end;
  73.         Delay(45, dummy);{Wait a while 3/4 of second}
  74.  
  75.         DisposeWindow(WIND); {Close up}
  76.         SetPort(OldPort);
  77.     end;
  78. {======================}
  79. end.
  80. {======================}